﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMPLib;

namespace CD_Reader
{
    public class CD_Reader
    {
      #region Fields (3) 

        IWMPCdrom cd;
        IWMPCdromRip rip ;
        WMPLib.WindowsMediaPlayer player;

      #endregion Fields 

      #region Constructors (2) 

        public CD_Reader(string inDriveLetter):this()
        {
            cd = player.cdromCollection.getByDriveSpecifier(inDriveLetter); 
        }

        public CD_Reader()
        {
            player = new WindowsMediaPlayer();
        
        }

      #endregion Constructors 

      #region Methods (12) 

      #region Public Methods (12) 

       public void EjectCD()
        {
           cd.eject();
         }

        public string GetArtist()
        {
            if (this.cd.Playlist.count > 0)
                return cd.Playlist.Item[0].getItemInfo("AlbumArtist");
            else
                return string.Empty;
        }

        public string GetCD_Title()
        {
            return cd.Playlist.name;
        }

        public string GetRipProgress()
        {
            return rip.ripProgress.ToString();
        }

        public string GetRipState()
        {
            string returnValue = string.Empty;
          
            switch (rip.ripState)
            {
                case WMPRipState.wmprsRipping:
                    returnValue = "Still Ripping";
                    break;
                case WMPRipState.wmprsStopped:
                    returnValue = "Done";
                    break;
                case WMPRipState.wmprsUnknown:
                    returnValue = "Unknown";
                    break;

            }
           return returnValue;
        }

        public List<string> GetTracks()
        {
           List<string> CurrentTracks = new List<string>();
            for (int i = 0; i < cd.Playlist.count; i++)
            {
                string trackName = cd.Playlist.Item[i].name;
                CurrentTracks.Add(trackName);
            }
            return CurrentTracks;
        }

        public List<string> GetUnburnedTracks (List<string> AlreadyBurnedTracks)
        {
           List<string> CurrentTracks = new List<string> ();
           for (int i = 0; i < cd.Playlist.count; i++)
           {
              string trackName = cd.Playlist.Item[i].name;
              if(! AlreadyBurnedTracks.Contains(trackName))
               CurrentTracks.Add (trackName);
           }
           return CurrentTracks;           
        }

        public bool isCD_Ready()
        {
           return  ! cd.Playlist.name.Equals("No disc in drive");
        }

        public List<string> ListAllAttributes()
        {
            List<string> myAttributes = new List<string>();
            if (cd != null && cd.Playlist.count > 0)
            {
               IWMPMedia song = cd.Playlist.Item[0];

               int numAttributes = song.attributeCount;
               for (int i = 0; i < numAttributes; i++)
               {
                  string s = song.getAttributeName(i) + "  |  " + song.getItemInfo(song.getAttributeName(i));
                  myAttributes.Add(s);
               }

               myAttributes.Add("Duration | " + song.durationString);
            }
            return myAttributes;
        }

        public void RipAllTracks()
        {
           rip = (IWMPCdromRip)cd;
            rip.startRip();
           
        }

        public void SetIndividualTrackToRip(IWMPMedia inTrack)
        {
            inTrack.setItemInfo("SelectedForRip", "True");
           
        }

        public void SetNewDriveLetter(string inNewDriveLetter)
        {
            cd = player.cdromCollection.getByDriveSpecifier(inNewDriveLetter);
        }

        public void SetTracksToRip(List<string> inTracksToRip)
        {

            for (int i = 0; i < cd.Playlist.count; i++)
            {
                if (inTracksToRip.Contains(cd.Playlist.Item[i].name))
                {
                    cd.Playlist.Item[i].setItemInfo("SelectedForRip", "True");
                }
                else
                {
                    cd.Playlist.Item[i].setItemInfo("SelectedForRip", "False");
                }
            }

        }

      #endregion Public Methods 

      #endregion Methods 
    }
}
